//
//  RadioHomeTableViewHeadView.m
//  SunXi
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RadioHomeTableViewHeadView.h"

@implementation RadioHomeTableViewHeadView{
    NSMutableArray *_SCArr;
    NSMutableArray *_COArr;
    DyScrollView *_scroView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
//MARK:设置瀑布流
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat Hei = (WH - 16) / 3;
    flowLayout.itemSize = CGSizeMake(Hei, Hei);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 3;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    flowLayout.footerReferenceSize = CGSizeMake(0, 10);
    self.backgroundColor = [UIColor redColor];
//MARK:设置collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WH, HT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(HT - Hei - 10, 0, 0, 0));
        
    }];
    
    self.SCView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WH, HT - Hei - 10)];
    
    
    [self.collectionView registerClass:[RadioHomeTableViewHeadViewCollectionViewCell class] forCellWithReuseIdentifier:@"RadioHomeTableViewHeadViewCollectionViewCell"];
    
    
    self.SCView = [[UIView alloc]init];
    [self addSubview:self.SCView];
    self.SCView.backgroundColor = [UIColor whiteColor];
    [self.SCView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, Hei + 10, 0));
        
    }];
    
    //轮播
    _scroView = [[DyScrollView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, HT - (Hei + 10))];
    [self.SCView addSubview:_scroView];
    _scroView.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(createData)
                                                 name: @"刷新轮播等"
                                               object: nil];
    
    
    [self createData];
    
}

- (void)createData{
    NSString *radioURL = @"http://api2.pianke.me/ting/radio";
    NSDictionary *para = @{@"client":@"2"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:radioURL WithParameters:para Block:^(id result) {
        
        _SCArr = [NSMutableArray array];
        _COArr = [NSMutableArray array];
        _SCArr = [RadioModel baseModelByArr:result[@"data"][@"carousel"]];
        _COArr = [RadioModel baseModelByArr:result [@"data"][@"hotlist"]];
        [self.collectionView reloadData];
        NSMutableArray *arr = [NSMutableArray array];
        for (RadioModel *temp in _SCArr) {
            NSString *url  = temp.coverimg;
            [arr addObject:url];
        }
        [_scroView createView:arr width:SWIDTH];
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _COArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RadioHomeTableViewHeadViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RadioHomeTableViewHeadViewCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    cell.model = _COArr[indexPath.item];
    return cell;
}
//轮播图点击方法:
- (void)takeValue:(NSInteger)TouchValue{
    [self.delegate TouchScrolltakeValue:[_SCArr[TouchValue] radioid]];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.delegate TouchCollectionViewtakeValue:_COArr[indexPath.item]];
}



@end
